package com.keste.adf.ui.template;

import com.keste.adf.ui.bean.session.GlobalInfo;
import com.keste.adf.ui.fwk.util.ADFUtils;

import com.keste.adf.ui.fwk.util.QuizUtils;

import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;
import oracle.adf.view.rich.component.rich.nav.RichCommandLink;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;

import oracle.binding.OperationBinding;

public class PageTemplateBean {
    
    private GlobalInfo globalInfo;
    private String currentUser;
    private RichInputText userName;
    private RichInputText userPassword;
    private RichPanelGroupLayout loginGroup;
    private RichCommandLink logout;

    public PageTemplateBean() {

        globalInfo = (GlobalInfo)QuizUtils.getGlobalInfo();
    }

    public void onLogout(DialogEvent dialogEvent) {
        if(dialogEvent.getOutcome().equals(DialogEvent.Outcome.yes))
        {
            globalInfo.getDBConnector().logout();
            
            
            OperationBinding resetCurrentUserMethod = ADFUtils.findOperationBinding("resetCurrentUser");
            if (resetCurrentUserMethod != null) {
                resetCurrentUserMethod.execute();
            }
            
            currentUser = null;
            
            AdfFacesContext.getCurrentInstance().addPartialTarget(this.getLoginGroup());
        }
    }

    public void onLogin(DialogEvent dialogEvent) {
        if(dialogEvent.getOutcome().equals(DialogEvent.Outcome.ok))
        {
            globalInfo.getDBConnector().doLogin((String)userName.getValue(), (String)userPassword.getValue());
            
            OperationBinding setCurrentUserMethod = ADFUtils.findOperationBinding("setCurrentUser");
            if (setCurrentUserMethod != null) {
                setCurrentUserMethod.getParamsMap().put("loginId", QuizUtils.getPageFlowScopeMap().get("userId"));
                setCurrentUserMethod.execute();
            }
            
            currentUser = (String)userName.getValue();
            userName.setValue(null);
            userPassword.setValue(null);
            AdfFacesContext.getCurrentInstance().addPartialTarget(this.getLoginGroup());
            
        }
    }

    public void onUserSignup(DialogEvent dialogEvent) {
        // Add event code here...
    }

    public void setUserName(RichInputText userName) {
        this.userName = userName;
    }

    public RichInputText getUserName() {
        return userName;
    }

    public void setUserPassword(RichInputText userPassword) {
        this.userPassword = userPassword;
    }

    public RichInputText getUserPassword() {
        return userPassword;
    }

    public void setLoginGroup(RichPanelGroupLayout loginGroup) {
        this.loginGroup = loginGroup;
    }

    public RichPanelGroupLayout getLoginGroup() {
        return loginGroup;
    }

    public void setLogout(RichCommandLink logout) {
        this.logout = logout;
    }

    public RichCommandLink getLogout() {
        return logout;
    }
}
